// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UITitleBar.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.UI.Forms
{
    #region Using Directives

    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    #endregion

    /// <summary>
    /// The ui title bar.
    /// </summary>
    public partial class UITitleBar : UIControl
    {
        #region Constants and Fields

        /// <summary>
        /// The size.
        /// </summary>
        private Size size = new Size(16, 200);

        /// <summary>
        /// The vertical.
        /// </summary>
        private bool vertical;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UITitleBar"/> class.
        /// </summary>
        public UITitleBar()
        {
            this.CalculateSize();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets Font.
        /// </summary>
        public override Font Font
        {
            get
            {
                return base.Font;
            }

            set
            {
                base.Font = value;
                this.CalculateSize();
            }
        }

        /// <summary>
        /// Gets or sets Text.
        /// </summary>
        public override string Text
        {
            get
            {
                return base.Text;
            }

            set
            {
                base.Text = value;
                this.CalculateSize();
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether Vertical.
        /// </summary>
        [Category("UIBaseLabel")]
        public bool Vertical
        {
            get
            {
                return this.vertical;
            }

            set
            {
                this.vertical = value;
                this.CalculateSize();
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The to string.
        /// </summary>
        /// <returns>
        /// The to string.
        /// </returns>
        public override string ToString()
        {
            return string.Format(
                "{0}x{1}:{2}:{3}", this.Width, this.Height, this.vertical ? "Vertical" : "Horizontal", this.Text);
        }

        #endregion

        #region Methods

        /// <summary>
        /// The on paint.
        /// </summary>
        /// <param name="e">
        /// The e.
        /// </param>
        protected override void OnPaint(PaintEventArgs e)
        {
            if (this.vertical)
            {
                var br = new SolidBrush(this.ForeColor);
                var smt = new StringFormat();

                smt.Alignment = StringAlignment.Center;

                var sizeTemp = new Size();
                var i = 0;
                foreach (var c in this.Text)
                {
                    sizeTemp = TextRenderer.MeasureText(c.ToString(), this.Font);
                    var rc = new Rectangle(0, sizeTemp.Height * i, sizeTemp.Width, sizeTemp.Height);
                    e.Graphics.DrawString(c.ToString(), this.Font, br, rc, smt);
                    i++;
                }

                br.Dispose();
            }
            else
            {
                var br = new SolidBrush(this.ForeColor);
                var smt = new StringFormat();
                smt.Alignment = StringAlignment.Center;
                var rc = new Rectangle(0, 0, this.Width, this.Height);
                e.Graphics.DrawString(this.Text, this.Font, br, rc, smt);
                br.Dispose();
            }
        }

        /// <summary>
        /// The calculate size.
        /// </summary>
        private void CalculateSize()
        {
            if (this.vertical)
            {
                var height = 0;
                var width = 0;

                foreach (var c in this.Text)
                {
                    var sizeTemp = TextRenderer.MeasureText(c.ToString(), this.Font);
                    height += sizeTemp.Height;

                    if (sizeTemp.Width > width)
                    {
                        width = sizeTemp.Width;
                    }
                }

                Size = new Size(width, height);
            }
            else
            {
                var s = TextRenderer.MeasureText(this.Text, this.Font);
                Size = new Size(s.Width + 20, s.Height);
            }
        }

        #endregion
    }
}